package edu.ustb.efws.base.mapper.type;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ustb.efws.base.domain.Alarm;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 报警点数据转换
 *
 * @author TS Group
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
public class AlarmPositionsTypeHandler extends BaseTypeHandler<List<Alarm.SimplePosition>> {
    private static final Logger LOGGER = LoggerFactory.getLogger(AlarmPositionsTypeHandler.class);

    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final JavaType POSITION_LIST =
            MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, Alarm.SimplePosition.class);

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<Alarm.SimplePosition> positions, JdbcType jdbcType) throws SQLException {
        try{
            String data = "{}";
            if(positions !=null && positions.isEmpty()){
                data = MAPPER.writeValueAsString(positions);
            }
            preparedStatement.setString(i, data);
        }catch (IOException e){
            LOGGER.error("Set position fail throw={}", e.getMessage());
            throw new SQLException("Set parameter convert positions error", e);
        }
    }

    @Override
    public List<Alarm.SimplePosition> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        try{
            String data = resultSet.getString(s);
            return MAPPER.readValue(data, POSITION_LIST);
        }catch (IOException e){
            LOGGER.error("Get position fail throw={}", e.getMessage());
            throw new SQLException("Get parameter convert positions error", e);
        }
    }

    @Override
    public List<Alarm.SimplePosition> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        try{
            String data = resultSet.getString(i);
            return MAPPER.readValue(data, POSITION_LIST);
        }catch (IOException e){
            LOGGER.error("Get position fail throw={}", e.getMessage());
            throw new SQLException("Get parameter convert positions error", e);
        }
    }

    @Override
    public List<Alarm.SimplePosition> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        try{
            String data = callableStatement.getString(i);
            return MAPPER.readValue(data, POSITION_LIST);
        }catch (IOException e){
            LOGGER.error("Get position fail throw={}", e.getMessage());
            throw new SQLException("Get parameter convert positions error", e);
        }
    }
}
